<?php

namespace common\services\config;

use common\repositories\config\ConfigSettingRepository;
use common\services\BaseService;
use common\support\CacheManage;

class ConfigSettingService extends BaseService
{
    private $_configSettingRepository;

    public function __construct()
    {
        $this->_configSettingRepository = new ConfigSettingRepository();
    }

    /**
     * 获取单个配置项值
     * @param mixed $key 
     * @return mixed
     */
    public function getValue($key)
    {
        return $this->_configSettingRepository->getValueByKey($key);
    }
    
    /**
     * 获取多个配置项值
     * @param mixed $keys 
     * @return mixed
     */
    public function getValues($keys)
    {
        return $this->_configSettingRepository->getValuesByKeys($keys);
    }
    public function getCacheValue($key,$jsonDecode=false){
        $configValue = CacheManage::getConfigSettingsData($key);
        if (empty($configValue)) {
            $repository = new ConfigSettingRepository();
            $result = $repository->getByKey($key);
            if (empty($result)) {
                return false;
            }
            $configValue = $result->Value;
            CacheManage::setConfigSettingsData($key, $configValue, 0);
        }
        return $jsonDecode?json_decode($configValue,true):$configValue;
    }
}